package com.kevin.Code.Math;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Vinlee Xiao
 * @Classname PascalsTriangle
 * @Description Leetcode 118杨辉三角 简单难度
 * @Date 2021/12/29 16:06
 * @Version 1.0
 */
public class PascalsTriangle {

    List<List<Integer>> result = new ArrayList<>();

    /**
     * 帕斯卡三角形
     *
     * @param numRows
     * @return
     */
    public List<List<Integer>> generate(int numRows) {

        //第一行和第二行属于特殊情况
        List<Integer> tmp = new ArrayList<>();
        tmp.add(1);
        result.add(new ArrayList<Integer>(tmp));
        if (numRows == 1) {

            return result;
        }
        for (int i = 2; i <= numRows; i++) {
            ArrayList<Integer> subList = new ArrayList();
            subList.add(1);
            List<Integer> res = result.get(result.size() - 1);
            for (int j = 1; j < res.size(); j++) {
                int sum = res.get(j) + res.get(j - 1);
                subList.add(sum);
            }
            subList.add(1);
            //将subList添加进result中
            result.add(new ArrayList<Integer>(subList));
        }

        return result;
    }

    public static void main(String[] args) {
        PascalsTriangle pascalsTriangle = new PascalsTriangle();

        List<List<Integer>> lists = pascalsTriangle.generate(5);
        System.out.println(lists.size());
        for (List<Integer> list : lists) {
            for (Integer e : list) {
                System.out.print(e + " ");
            }
            System.out.println();
        }

    }
}
